<?php

/**
 * This is the model class for table "challenge".
 *
 * The followings are the available columns in table 'challenge':
 * @property integer $id
 * @property string $name
 * @property string $begin_date
 * @property string $end_date
 * @property string $status
 * @property integer $challenger_id
 * @property integer $mediator_id
 * @property string $mediator_note
 *
 * The followings are the available model relations:
 * @property User $challenger
 * @property User $mediator
 * @property ChallengeHasProposal[] $challengeHasProposals
 */
class Challenge extends CActiveRecord
{
	public static function getStatusList($key) {
		$listStatus = array(
				'NOT_CONFIRMED' => 'Não Confirmado',
				'IN_PROGRESS' => 'Em Andamento',
				'RESOLVED' => 'Resolvido',
				'CONTESTED' => 'Contestado',
				'CLOSED' => 'Encerrado',
		);
	
		return $listStatus[$key];
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'challenge';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, mediator_id, begin_date, challenger_id', 'required'),
			array('challenger_id, mediator_id', 'numerical', 'integerOnly'=>true),
			array('name, mediator_note', 'length', 'max'=>250),
			array('status', 'length', 'max'=>45),
			array('end_date', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, begin_date, end_date, status, challenger_id, mediator_id, mediator_note', 'safe', 'on'=>'search'),
		);
	}

	public function delete() {
		$array = $this->challengeHasProposals;
		if(!is_null($array) && !empty($array)) {
			foreach ($array as $item) {
				$item->delete();
			}
		}
		
		parent::delete();
	}
	
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'challenger' => array(self::BELONGS_TO, 'User', 'challenger_id'),
			'mediator' => array(self::BELONGS_TO, 'User', 'mediator_id'),
			'challengeHasProposals' => array(self::HAS_MANY, 'ChallengeHasProposal', 'challenge_id'),
		);
	}
	
	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'name' => 'Nome',
				'begin_date' => 'Início',
				'end_date' => 'Fim',
				'status' => 'Estado',
				'challenger_id' => 'Desafiante',
				'mediator_id' => 'Mediador',
				'mediator_note' => 'Nota do Mediador',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('begin_date',$this->begin_date,true);
		$criteria->compare('end_date',$this->end_date,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('challenger_id',$this->challenger_id);
		$criteria->compare('mediator_id',$this->mediator_id);
		$criteria->compare('mediator_note',$this->mediator_note,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Challenge the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}

